var common = require('../controller/commonController.js');

var InhouseController = require('../controller/InhouseController.js');
var OuthouseController = require('../controller/OuthouseController.js');

class TotalController{
	constructor() {
		this.inhouseController = new InhouseController();
		this.outhouseController = new OuthouseController();
	}

	computeDataByNum(inhouseData,outhouseData,callback){
		let currOutData = new Array();
		for(let i = 0;i < inhouseData.length;i ++){
			currOutData = selectOrderByInhouse(inhouseData[i],outhouseData);
			for(let j = 0;j < currOutData.length;j ++){
		 		if (inhouseData[i].currently_num <= 0) {
		        	break;
		        }
		        //当入库量小于出库量时，将出库量进行拆分即新增一条数据
		        if ((currOutData[j].num - inhouseData[i].currently_num) > 0) {
		        	//新增数据并添加到待处理的数据中
		        	let reminderData = new Object();
					reminderData = currOutData[j];
					reminderData.num = currOutData[j].num - inhouseData[i].currently_num;
					reminderData.remarks="splitedData";
					reminderData.id = common.getMaxID(outhouseData)+1;
		        	currOutData.push(reminderData);
	 
					//处理原有的数据项
					currOutData[j].num = inhouseData[i].currently_num;
					currOutData[j].inhouse_id = inhouseData[i].id;
					currOutData[j].inhouse_date = inhouseData[i].inhouse_date;
					currOutData[j].remarks="beforesplit";
					inhouseData[i].currently_num = 0;//强制出库数置为0
					break;
				}
				inhouseData[i].currently_num = inhouseData[i].currently_num - currOutData[j].num;			
		 		currOutData[j].inhouse_id = inhouseData[i].id;
		 		currOutData[j].inhouse_date = inhouseData[i].inhouse_date;
		 	}
		 	common.updateArrayData(currOutData,outhouseData);
	 	}

	 	let totalArr = new Array();
	 	totalArr[0] = inhouseData;
	 	totalArr[1] = outhouseData;
	 	callback(totalArr);
	}
}

//根据入库单数据筛选出库单
function selectOrderByInhouse(oneInhouse,outhouse){
	let resArr = new Array(); //根据searchLsit的条件以及对应值校验后符合条件的数据

	let param = new Object();
	let searchList = ['tenant_name','warehouse_num','commodity_name','inhouse_date','partner_name_a','partner_name_b','partner_name_c'];
	searchList.forEach((value)=>{
	    param[value] = oneInhouse[value];
	});

	//判断该条数据是否符合条件并且未被处理
	for(let i = 0;i < outhouse.length;i++){
		let inParterArr = new Array(param.partner_name_a,param.partner_name_b,param.partner_name_c);
		let outParterArr = new Array(outhouse[i].partner_name_a,outhouse[i].partner_name_b,outhouse[i].partner_name_c);
		
		if (outhouse[i].inhouse_id !== null) {
			continue;
		}
		if (!common.partnerMatch(inParterArr,outParterArr)){
			continue;
		}
		if (param.tenant_name !== outhouse[i].outhouse_name) {
			continue;
		}
		if (outhouse[i].warehouse_num !== param.warehouse_num) {
			continue;
		}
		if (outhouse[i].commodity_name !== param.commodity_name) {
			continue;
		}
		if (!common.compareDate(param.inhouse_date, outhouse[i].outhouse_date)) {
			continue;
		}
		resArr.push(outhouse[i]);//找到符合条件的数据
	}

	resArr.sort(common.compareObjectDate());

	return resArr;
}

module.exports = TotalController;